﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;

using Safe_Driving.TokenUser;
using Safe_Driving.EasyCalendar.Controler;
using System.Collections;
using System.Windows.Input;

namespace Safe_Driving
{
    public partial class MainWindow
    {
        #region Window EVT

        private void btn_add_indispos_Click(object sender, RoutedEventArgs e)
        {
            //Récupération de la DB
            SingletonTokenUser tok = SingletonTokenUser.GetInstance();
            AppSafeDriving dbApp = tok.DbApp;

            //initalisation des listes utilisées
            List<SFInDiSpOnIbiLite> lst_indispos = new List<SFInDiSpOnIbiLite>();
            lst_indispos = (List<SFInDiSpOnIbiLite>)dataGrid_indispos_moniteur.ItemsSource;
            IEnumerator lst_indisposEnumerator = lst_indispos.GetEnumerator();

            while (lst_indisposEnumerator.MoveNext())
            {
                SFInDiSpOnIbiLite indispo = ((SFInDiSpOnIbiLite)lst_indisposEnumerator.Current);
                indispo.IDMonItEUR = tok.Moniteur_connected.IDMonItEUR;

                dbApp.SFInDiSpOnIbiLite.InsertOnSubmit(indispo);
            }
            
            dbApp.SubmitChanges();
            this.Pane_indispos_moniteur.Visibility = Visibility.Hidden;
            //this.EC_Moniteur_indispo.jump_week(0);
        }

        private void img_close_indispo_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.Pane_indispos_moniteur.Visibility = Visibility.Hidden;
        }
        
        #endregion



        /// <summary>
        /// Permet de générer l'ItemsSource du datagrid
        /// </summary>
        /// <param name="list_final"></param>
        /// <param name="dayDate"></param>
        public void manage_indispos(List<int> list_final, DateTime dayDate)
        {
            //On affiche le panneau de configuration des indispos à ajouter
            Pane_indispos_moniteur.Visibility = Visibility.Visible;

            //On initialise les variables de collection qui serviront à remplir le datagrid dataGrid_indispos_moniteur
            IEnumerator list_finalEnumerator = list_final.GetEnumerator();
            int last_index, index_current;

            DateTime debut, fin;
            List<SFInDiSpOnIbiLite> lst_indispos = new List<SFInDiSpOnIbiLite>();

            if (list_final.Count == 2)
            {
                index_current = (int)list_finalEnumerator.Current;
                list_finalEnumerator.MoveNext();
                last_index = (int)list_finalEnumerator.Current;

                debut = DateTime.Parse(dayDate.Year + "/" + dayDate.Month + "/" + dayDate.Day + " " + index_current.ToString() + ":00:00");
                fin = DateTime.Parse(dayDate.Year + "/" + dayDate.Month + "/" + dayDate.Day + " " + (last_index + 1).ToString() + ":00:00");
                lst_indispos.Add(createIndispos(debut, fin));

            }
            else
            {
                //Move other the 0 index
                list_finalEnumerator.MoveNext();

                while (list_finalEnumerator.MoveNext())
                {
                    index_current = (int)list_finalEnumerator.Current;
                    list_finalEnumerator.MoveNext();
                    last_index = (int)list_finalEnumerator.Current;

                    debut = DateTime.Parse(dayDate.Year + "/" + dayDate.Month + "/" + dayDate.Day + " " + index_current.ToString() + ":00:00");
                    fin = DateTime.Parse(dayDate.Year + "/" + dayDate.Month + "/" + dayDate.Day + " " + (last_index + 1).ToString() + ":00:00");
                    lst_indispos.Add(createIndispos(debut, fin));
                }
            }

            dataGrid_indispos_moniteur.ItemsSource = lst_indispos;

        }


        /// <summary>
        /// Genére une objet SFInDiSpOnIbiLite
        /// </summary>
        /// <param name="debut"></param>
        /// <param name="fin"></param>
        /// <returns></returns>
        private SFInDiSpOnIbiLite createIndispos(DateTime debut, DateTime fin)
        {
            SFInDiSpOnIbiLite s = new SFInDiSpOnIbiLite();
            s.DateDebut = debut;
            s.DateFIn = fin;

            return s;
        }

        public void delete_indispo(int id_cours)
        {
            SingletonTokenUser tok = SingletonTokenUser.GetInstance();

            SFInDiSpOnIbiLite ind = (from c in tok.DbApp.SFInDiSpOnIbiLite
                             where c.IDInDiSpOnIbiLite == id_cours
                             select c).SingleOrDefault();

            tok.DbApp.SFInDiSpOnIbiLite.DeleteOnSubmit(ind);
            tok.DbApp.SubmitChanges();
            //this.EC_Moniteur_indispo.jump_week(0);
        }
    }
}
